package org.kl.bf.entity.easyui;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.kl.bf.entity.basic.Menu;
import org.kl.bf.web.base.BasicController;
import org.springframework.util.StringUtils;
import org.springside.modules.utils.Collections3;

public class EasyuiUtils implements java.io.Serializable {

	private static final long serialVersionUID = -6947765522709203690L;

	/**
	 * 根据Menu类构造树形菜单
	 * 
	 * @param menu
	 */
	public static EasyuiTreeNode buildMenuTree(Menu menu, String contextPath) {
		EasyuiTreeNode node = new EasyuiTreeNode();
		node.setId(menu.getId());
		node.setText(menu.getMenuName());
		node.setIconCls(menu.getIconClass());
		node.setPid(menu.getMenuParentId());
		node.setDisplayOrder(menu.getDisplayOrder());
		Map<String, Object> attributes = new HashMap<String, Object>();
		attributes.put("href", StringUtils.isEmpty(menu.getMenuUrl()) ? "" : contextPath + menu.getMenuUrl());
		node.setAttributes(attributes);
		return node;
	}
	
	/**
	 * 根据微信Menu类构造树形菜单
	 * 
	 * @param menu
	 *//*
	public static EasyuiTreeNode buildWxMenuTree(WxMenu wxMenu, String contextPath) {
		EasyuiTreeNode node = new EasyuiTreeNode();
		node.setId(wxMenu.getId());
		node.setText(wxMenu.getMenuName());
		node.setPid(wxMenu.getMenu().getId());
		Map<String, Object> attributes = new HashMap<String, Object>();
		attributes.put("menuType", wxMenu.getMenuType());
		attributes.put("menuKey", wxMenu.getMenuKey());
		attributes.put("menuUrl", StringUtils.isEmpty(wxMenu.getMenuUrl()) ? "" : contextPath + wxMenu.getMenuUrl());
		attributes.put("mediaId", wxMenu.getMediaId());
		attributes.put("app", wxMenu.getApp());
		//attributes.put("href", StringUtils.isEmpty(menu.getMenuUrl()) ? "" : contextPath + menu.getMenuUrl());
		node.setAttributes(attributes);
		return node;
	}*/

	public static String buildEasyuiGridJsonString(List<?> list, int count) {
		if (Collections3.isEmpty(list)) {
			return "{\"total\":" + count + ",\"rows\":[]}";
		} else {
			EasyuiGrid grid = new EasyuiGrid();
			grid.setRows(list);
			grid.setTotal(count);
			return BasicController.jsonMapper.toJson(grid);
		}
	}

	public static String buildEasyuiGridJsonString(List<? extends Serializable> list, List<? extends Serializable> footerList, int count) {
		if (Collections3.isEmpty(list)) {
			return "{\"total\":" + count + ",\"rows\":[],\"footer\":[{}]}";
		} else {
			EasyuiGrid grid = new EasyuiGrid();
			grid.setRows(list);
			grid.setTotal(count);
			grid.setFooter(footerList);
			return BasicController.jsonMapper.toJson(grid);
		}
	}
}
